package com.atguigu.jxc.controller;


import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //http://localhost:8080/goods/listInventory
    //查询当前库存（可根据商品类别、商品编码或名称搜索）
    @PostMapping("/listInventory")
    public Map<String,Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId){
        Map<String,Object> map=new HashMap<>();
        Page<Goods> goodsPage =goodsService.listInventory(page,rows,codeOrName,goodsTypeId);
        map.put("rows",goodsPage);
        map.put("total",goodsPage.getTotal());
        return map;
    }

    //http://localhost:8080/goods/list
    ////查询所有商品信息,根据分类、名称查询
    @PostMapping("list")
    public Map<String,Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
       return goodsService.list(page,rows,goodsName,goodsTypeId);
    }


    //http://localhost:8080/goods/save?goodsId=37
    //商品添加或修改
    @PostMapping("/save")
    public ServiceVO save(Goods goods){
        try {
            goodsService.save(goods);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/goods/delete
    //商品删除（要判断商品状态,入库、有进货和销售单据的不能删除）
    @PostMapping("/delete")
    public ServiceVO delete(Integer goodsId){
        try {
            goodsService.delete(goodsId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.STORED_ERROR_CODE, ErrorCode.STORED_ERROR_MESS);
        }
    }

    //http://localhost:8080/goods/getNoInventoryQuantity
    //无库存商品列表展示（可以根据商品名称或编码查询）
    @PostMapping("/getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
          return goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
    }

    //http://localhost:8080/goods/getHasInventoryQuantity
    //有库存商品列表展示（可以根据商品名称或编码查询）
    @PostMapping("/getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        return goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
    }

    //http://localhost:8080/goods/saveStock?goodsId=25
    //添加库存、修改数量或成本价
    @PostMapping("saveStock")
    public ServiceVO saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice){
        try {
            goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/goods/deleteStock
    //删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
    @PostMapping("/deleteStock")
    public ServiceVO deleteStock(Integer goodsId){
        try {
            goodsService.deleteStock(goodsId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.HAS_FORM_ERROR_CODE, ErrorCode.HAS_FORM_ERROR_MESS);
        }
    }

    //http://localhost:8080/goods/listAlarm
    //查询所有 当前库存量 小于 库存下限的商品信息
    //库存报警
    @PostMapping("/listAlarm")
    public Map<String,Object> listAlarm(){
       return goodsService.listAlarm();
    }
}
